<?php

/**
 * 用户举报
 *
 */
use Common\Bridge;
use Center\UserReport as UR;
use Circle\Topic;

define('SITE_AUTH', 'report-reports');
require('../../../common.inc.php');

class reports extends \lib\core\AdminAction
{
    const TABLE = 'UserReport';

    public function doDefault()
    {
        $user_id = intval($_GET['user_id']);
        $itemtype = trim($_GET['itemtype']);
        $itemid = intval($_GET['itemid']);
        $status = intval($_GET['status']);
        $page = max(1, intval($_GET['page']));

        $where = [];
        if (!empty($user_id)) {
            $where['UserId'] = $user_id;
        }
        if (is_numeric($itemtype)) {
            $itemtype = intval($itemtype);
            $where['Type'] = $itemtype;
        }
        if (!empty($itemid)) {
            $where['ItemId'] = $itemid;
        }
        if (!empty($status)) {
            $where['Status'] = $status;
        }

        $pdo_r = Bridge::pdo(Bridge::DB_CENTER_R);
        $total = $pdo_r->clear()->select('count(UserId)')->from(self::TABLE)->where($where)->getValue();
        $smarty = $this->app->page();
        if ($total > 0)
        {
            $data = $pdo_r->clear()
                ->select('*')
                ->from(self::TABLE)
                ->where($where)
                ->order('ReportId DESC')
                ->limit(($page - 1) * self::PAGE_SIZE, self::PAGE_SIZE)
                ->getAll();

            $title = [];
            if (!empty($data)) {
                $ids_topic = [];
                $ids_comm = [];
                foreach ($data as $k => $v) {
                    if ($v['ItemType'] == 1) {
                        $ids_topic[] = $v['ItemId'];
                    }
                    if($v['ItemType'] == 2){
                        $ids_comm[] = $v['ItemId'];
                    }
                }
//                pre($ids_comm).exit();
                $info_topic = Topic::getInfoByIds($ids_topic);//文章
                $info_comm = \Circle\Comment::getInfoByIds($ids_comm);//评论
                foreach ($info_topic as $k => $v) {
                    $title[$v['TopicId']] = $v['Description'];
                }
                foreach ($info_comm as $k => $v){
                    $title[$v['CommentId']] = base64_decode($v['Content']);
                }
            }

            $ItemTypes = [
                1 => '帖子',
                2 => '评论',
                3 => '其他'
            ];
            $Status = [
                1 => '待解决',
                2 => '不解决',
                3 => '已解决'
            ];

            $smarty->value('data', $data);
            $smarty->value('title', $title);
            $smarty->value('ItemType',$ItemTypes);
            $smarty->value('Status',$Status);
            $pagination = $this->pagination($total, self::PAGE_SIZE, $page);
            $smarty->value('pagination', $pagination);
        }

        $types = [
            0 => '其他',
            1 => '色情/暴力信息',
            2 => '广告信息',
            3 => '钓鱼/欺诈信息',
            4 => '诽谤造谣信息',
            10 => '用户举报'
        ];


        $smarty->value('type',$types);
        $smarty->value('user_id', $user_id);
        $smarty->value('itemtype', $itemtype);
        $smarty->value('itemid', $itemid);
        $smarty->value('status', $status);
        $smarty->value('total', $total);
        $smarty->output('report/report.tpl');
    }


    public function doUpdateStatus()
    {
        $id = intval($_POST['id']);
        $type = intval($_POST['status']);
        $parameter = ['Status' => $type];
        $rs = UR::updateUserReport($id, $parameter);
        if ($rs) {
            $this->adminLog('更新反馈状态', $id, 'update', serialize(['id' => $id,$parameter]));
            $this->output(['title' => '操作成功', 'type' => 'success']);
        } else {
            $this->output(['title' => 'errors', 'type' => 'error']);
        }
    }


}
$app->run();